package com.myfum.mobile.tetris.views
{
	import com.myfum.mobile.tetris.Config;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Shape;
	import flash.display.Sprite;
	
	public class ScreenDrawer extends Sprite
	{
		private var bitmap:Bitmap;
		private var grid:Shape;
		
		public function ScreenDrawer()
		{
			init();
			drawGrid();
		}
		
		private function init():void{
			bitmap = new Bitmap(new BitmapData(Config.STAGE_WIDTH*Config.CUBE_SMALL_SCREEN, Config.STAGE_HEIGHT*Config.CUBE_SMALL_SCREEN, true, 0xffffff));
			bitmap.x = 250;
			bitmap.y = 170;
			addChild(bitmap);
		}
		
		private function drawGrid():void{
			grid = new Shape();
			grid.x = 250;
			grid.y = 170;
			addChild(grid);
			
			grid.graphics.beginFill(0xffcc00, .3);
			grid.graphics.drawRect(0, 0, Config.CUBE_SMALL_SCREEN*Config.STAGE_WIDTH, Config.CUBE_SMALL_SCREEN*Config.STAGE_HEIGHT);
			grid.graphics.endFill();
			
			grid.graphics.lineStyle(1, 0, .7);
			for(var i:int = 0;i<Config.STAGE_WIDTH+1;i++){
				grid.graphics.moveTo(i*Config.CUBE_SMALL_SCREEN, 0);
				grid.graphics.lineTo(i*Config.CUBE_SMALL_SCREEN, Config.CUBE_SMALL_SCREEN*Config.STAGE_HEIGHT);
			}
			for(i = 0;i<Config.STAGE_HEIGHT+1;i++){
				grid.graphics.moveTo(0, Config.CUBE_SMALL_SCREEN*i);
				grid.graphics.lineTo(Config.STAGE_WIDTH*Config.CUBE_SMALL_SCREEN, Config.CUBE_SMALL_SCREEN*i);
			}
		}
		
		public function clear():void{
			removeChild(bitmap);
			bitmap.bitmapData.dispose();
			bitmap = new Bitmap(new BitmapData(Config.STAGE_WIDTH*Config.CUBE_SMALL_SCREEN, Config.STAGE_HEIGHT*Config.CUBE_SMALL_SCREEN, true, 0xffffff));
			addChild(bitmap);
		}
		
		public function getBitmap():Bitmap{
			return bitmap;
		}
	}
}